package com.sg.socket;

import com.alibaba.fastjson2.JSON;

import com.sg.entity.Bqxx;
import com.sg.entity.Dzwl;
import com.sg.mapper.BqxxMapper;
import com.sg.mapper.DzwlMapper;
import com.sg.service.IntegralFlowService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

@Component
public class SocketClientStarter {

    @Resource
    private BqxxMapper bqxxMapper;
    @Resource
    private DzwlMapper dzwlMapper;
    @Resource
    private IntegralFlowService integralFlowService;

    @PostConstruct
    public void socketClient(){
        Thread jzThread = new Thread(() -> jz());
        Thread dzwlThread = new Thread(() -> dzwl());
        jzThread.start();
        dzwlThread.start();
    }




    public void jz(){
        Socket socket = null;
        try {
            socket = new Socket("115.236.153.177", 10181);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String line = null;
        String result = null;
        while (true) {
            try {
                if ((line = in.readLine()) == null){
                    socket = new Socket("115.236.153.177", 10181);
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    continue;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (result != null) {
                result = result + line;
            } else {
                result = line;
            }
            if (result.contains("}")) {
                String[] split = result.split("}");

                String bb = split[0] + "}";
                //存入数据库
                System.out.println(bb);
                Bqxx bqxx = JSON.parseObject(bb, Bqxx.class);
                bqxxMapper.insert(bqxx);
                if(split.length>1){
                    result = split[1];
                }else{
                    result = "";
                }
            }
        }
    }

    public void dzwl() {
        Socket socket = null;
        try {
            socket = new Socket("115.236.153.177", 50769);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String line = null;
        String result = null;
        while (true) {
            try {
                if ((line = in.readLine()) == null) {
                    socket = new Socket("115.236.153.177", 50769);
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    continue;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (result != null) {
                result = result + line;
            } else {
                result = line;
            }
            if (result.contains("}")) {
                String[] split = result.split("}");

                String bb = split[0] + "}";
                //存入数据库
                System.out.println(bb);
                Dzwl dzwl = JSON.parseObject(bb, Dzwl.class);
                dzwlMapper.insert(dzwl);
                // 判断是否需要增加积分   规则  每个位置只能获取一次积分
                integralFlowService.saveFlow(dzwl);
                if(split.length>1){
                    result = split[1];
                }else{
                    result = "";
                }
            }
        }
    }


}
